<section class="content-header">
    <h1>
        <?php echo $this->_lang["visitor_stats"]; ?>
        <small>Welcome</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="./"><i class="fa fa-dashboard"></i> Dashboard</a></li>
        <li class="active"><i class="fa fa-bar-chart-o"></i> <?php echo $this->_lang["visitor_stats"]; ?></li>
    </ol>
</section>

<!-- Main content -->
<section class="content">
    <div class="nav-tabs-custom">
        <!-- Tabs within a box -->
        <ul class="nav nav-tabs">
            <li class="active"><a href="#summary" data-toggle="tab"><?php echo $this->_lang['stats_summary_tab']; ?></a></li>
            <li><a href="#document-stats" data-toggle="tab"><?php echo $this->_lang['stats_documents_tab']; ?></a></li>
            <li><a href="#entry-stats" data-toggle="tab"><?php echo $this->_lang['stats_entry_tab']; ?></a></li>
            <li><a href="#referrer-stats" data-toggle="tab"><?php echo $this->_lang['stats_referrers_tab']; ?></a></li>
            <li><a href="#user-agent-stats" data-toggle="tab"><?php echo $this->_lang['stats_ua_tab']; ?></a></li>
            <li><a href="#os-stats" data-toggle="tab"><?php echo $this->_lang['stats_os_tab']; ?></a></li>
            <li><a href="#hosts-stats" data-toggle="tab"><?php echo $this->_lang['stats_hosts_tab']; ?></a></li>
        </ul>
        <div class="tab-content">
        <?php
		// figure out what the user wants to look at, and find the corresponding start and end times
		if(isset($_REQUEST['scope']) && isset($_REQUEST['start'])) {
		  $startdate = $_REQUEST['start'];
		  $scope = $_REQUEST['scope'];
		  $date = getdate($startdate);
		  $mon = $date['mon'];
		  $year = $date['year'];
		  switch($scope) {
			case "month" :
			  $enddate = mktime(23, 59, 59, $mon, date('t', $startdate), $year);
			break;
			case "day" :
			  $enddate = mktime(23, 59, 59, $mon, date('d', $startdate), $year);
			break;
		  }
		} else {
		  $startdate = 0;
		  $sql = "SELECT MIN(timestamp) AS first FROM ".$this->db."log_access";
		  $rs = $this->dbQuery($sql);
		  $tmp = $this->fetchRow($rs);
		  $startdate = $tmp['first']-1;
		  $enddate = time()+$this->config['server_offset_time'];
		  $scope = 'total';
		}
		// check to see the db's not empty...
		$sql = "SELECT timestamp FROM ".$this->db."log_access LIMIT 1";
		$rs = $this->dbQuery($sql);
		$count = $this->recordCount($rs);
		if($count==0) {
		?>
		<h2><?php echo $this->_lang['no_logging_found']; ?></h2>
		<?php } else { ?>
		  <?php
			// get page impressions for all time
			$sql = "SELECT COUNT(*) FROM ".$this->db."log_access WHERE timestamp > $startdate AND timestamp < $enddate ";
			$rs = $this->dbQuery($sql);
			$tmp = $this->fetchRow($rs);
			$pitotal = $tmp['COUNT(*)'];
		
			// get visits for all time
			$sql = "SELECT COUNT(*) FROM ".$this->db."log_access WHERE entry='1' AND timestamp > $startdate AND timestamp < $enddate ";
			$rs = $this->dbQuery($sql);
			$tmp = $this->fetchRow($rs);
			$vitotal = $tmp['COUNT(*)'];
		
			// get visitors for all time
			$sql = "SELECT COUNT(DISTINCT(visitor)) FROM ".$this->db."log_access WHERE timestamp > $startdate AND timestamp < $enddate ";
			$rs = $this->dbQuery($sql);
			$tmp = $this->fetchRow($rs);
			$vistotal = $tmp['COUNT(DISTINCT(visitor))'];
			// now show pages
			$toplimit = !isset($this->config['top_howmany']) || !is_numeric($this->config['top_howmany']) ? 10 : $this->config['top_howmany'] ; // figure out the top_limit
			?>
            <div id="summary" class="tab-pane active" style="position: relative;">
            	<?php include('views/summary_stats.phtml'); ?>
            </div>
            <div id="document-stats" class="tab-pane" style="position: relative;">
				<?php include('views/document_stats.phtml'); ?>
            </div>
            <div id="entry-stats" class="tab-pane" style="position: relative;">
                <?php include('views/entry_stats.phtml'); ?>
            </div>
            <div id="referrer-stats" class="tab-pane" style="position: relative;">
                <?php include('views/referrer_stats.phtml'); ?>
            </div>
            <div id="user-agent-stats" class="tab-pane" style="position: relative;">
                <?php include('views/user_agent_stats.phtml'); ?>
            </div>
            <div id="os-stats" class="tab-pane" style="position: relative;">
                <?php include('views/os_stats.phtml'); ?>
            </div>
            <div id="hosts-stats" class="tab-pane" style="position: relative;">
                <?php include('views/hosts_stats.phtml'); ?>
            </div>
        <?php } ?>
        </div>
    </div>
</section>